<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\User;

use App\Controller\AppBaseController;
use App\Model\Notify;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\WxApiAuth;

#[Controller(prefix: 'user')]
#[Middleware(WxApiAuth::class)]
class NotifyController extends AppBaseController
{
    /**
     * 通知--消息通知列表
     * @param int $module   通知模块 1：资讯 2：支部任务 3：在线活动
     * @return object
     */
    #[RequestMapping(path:'notifyList/{module}',methods: 'get')]
    public function notifyList(int $module): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $where['module'] = $module;
            $where['uid'] = $_user->id;
            if($module==1){
                //资讯
                $listModel = Notify::with(['news'=>function($query){
                    $query->selectRaw('id,type,title,link_id,link_url,created_at')->get();
                }])->where($where);
            }else if($module==2){
                //支部任务
                $listModel = Notify::with(['task'=>function($query){
                    $query->selectRaw('id,type,title,created_at')->get();
                }])->where($where);
            }else if($module==3){
                //活动
                $listModel = Notify::with(['act'=>function($query){
                    $query->selectRaw('id,type,title,created_at')->get();
                }])->where($where);
            }else{
                $listModel = Notify::where($where);
            }
            $list = $listModel->selectRaw('id,module,relation_id,uid,status,created_at')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();
            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>=$limit) $next = 1;
            }
            //判断是否可以发布
            $author_status = 0;
            if(in_array($_user->user_type,[8,9])){
                $author_status = 1;
            }

            return $this->reMsg->re(1,'success',[
                'list' => $list,
                'next' => $next,
                'author_status'=>$author_status
            ]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 通知--消息通知数量
     * @return object
     */
    #[RequestMapping(path:'notifyCount',methods: 'get')]
    public function notifyCount(): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $newsCount = 0;//资讯未处理总数
            $taskCount = 0;//支部任务未处理总数
            $actCount = 0;//活动未处理总数
            $where['status'] = 0;
            $where['uid'] = $_user->id;
            $notifyNoDoList = Notify::where($where)->get();
            if($notifyNoDoList->isNotEmpty()){
                foreach ($notifyNoDoList as $key=>$value){
                    if($value->module==1) $newsCount++;
                    if($value->module==2) $taskCount++;
                    if($value->module==3) $actCount++;
                }
            }

            return $this->reMsg->re(1,'success',[
                'news_count' => $newsCount,
                'task_count' => $taskCount,
                'act_count' => $actCount,
            ]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 通知|阅读
     * @param int $notify_id
     * @return object
     */
    #[RequestMapping(path:'notifyRead/{notify_id}',methods: 'get')]
    public function notifyRead(int $notify_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $where['id'] = $notify_id;
            $where['uid'] = $_user->id;
            $notifyInfo = Notify::where($where)->first();
            if($notifyInfo && $notifyInfo->status==0){
                $notifyInfo->status = 1;
                $notifyInfo->save();
            }
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

}